package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.activity.LoginActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;

public class SettingsActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout linearLayout;

    private static int tabs[] = {R.mipmap.jcgx,
            R.mipmap.qchc,
            R.mipmap.cjwt,
            R.mipmap.yjfk,
            R.mipmap.gywm_};
    private static String texts[] = {"检查更新", "清除缓存", "常见问题", "意见反馈", "关于我们"};

    private SharedPreferences mSharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mSharedPreferences = BaseApplication.mSharedPreferences;

        initViews();
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("设置");

        findViewById(R.id.mine_setting_exit_login).setOnClickListener(this);

        linearLayout = ((LinearLayout) findViewById(R.id.mine_setting_layout));
        for (int i = 0; i < 5; i++) {
            initItems(i);
        }
    }

    private void initItems(int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_settings, null);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        view.setBackgroundColor(Color.WHITE);
        ImageView imageView = (ImageView) view.findViewById(R.id.mine_setting_image);
        TextView textView = (TextView) view.findViewById(R.id.mine_settint_text);
        imageView.setImageResource(tabs[i]);
        textView.setText(texts[i]);
        if (i == 4) {
            view.findViewById(R.id.mine_setting_fengexian).setVisibility(View.GONE);
        }
        linearLayout.addView(view);
        switch (i) {
            case 2:
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(SettingsActivity.this, FAQActivity.class));
                    }
                });
                break;
            case 3:
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(SettingsActivity.this, FeedbackActivity.class));
                    }
                });
                break;
            case 4:
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(SettingsActivity.this, AboutUsActivity.class));
                    }
                });
                break;
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
            case R.id.mine_setting_exit_login:
                exitLogin();
                setResult(2);
                finish();
                startActivity(new Intent(SettingsActivity.this, LoginActivity.class));
                break;


        }
    }

    private void exitLogin() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean("isLogin", false);
        editor.putString("uid", "");
        editor.commit();
    }
}
